package com.third.bank.service.impl;

import cn.hutool.cache.Cache;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.third.bank.entity.SMSCode;
import com.third.bank.entity.User;
import com.third.bank.mapper.UserMapper;
import com.third.bank.service.SMSCodeService;
import com.third.bank.util.BaseResponse;
import com.third.bank.util.CodeUtils;
import com.third.bank.util.ErrorMessage;
import com.third.bank.util.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

@Service
public class SMSCodeServiceimpl implements SMSCodeService {

    @Autowired
    private CodeUtils codeUtils;

    /**
     * 生成验证码
     *
     * @param tele
     * @return
     */
    @Override
    @CachePut(value = "smsCode", key = "#tele" )
    public String sendCodeToSMS(String tele) {
//        生成验证码
        String s = codeUtils.sendCode(tele);
        try {
            codeUtils.getCode(tele);
            return s;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }

    /**
     * 检测验证码
     *
     * @param smsCode
     * @return
     */
    @Override
    public boolean checkCode(SMSCode smsCode) {
        System.out.println(smsCode);
        String code = codeUtils.getCode(smsCode.getTele());
        String nowCode = smsCode.getCode();
        return nowCode.equals(code);
    }
}
